//
//  JCClubSetupVC.m
//  SquashSpark
//
//  Created by 郭吉成 on 2017/11/28.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "JCClubSetupVC.h"
#import "ClubsSetting.h"
#import "JCClubApi.h"
#import "JCClubModel.h"

@interface JCClubSetupVC ()
@property (weak, nonatomic) IBOutlet JCSlider *dateSlider;
@property (weak, nonatomic) IBOutlet UILabel *dateValueLabel;
@property (weak, nonatomic) IBOutlet JCSlider *swingSlider;
@property (weak, nonatomic) IBOutlet UILabel *swingValueLabel;
@property (weak, nonatomic) IBOutlet JCSlider *durationSlider;
@property (weak, nonatomic) IBOutlet UILabel *durationValueLabel;
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;

@property (assign, nonatomic) NSInteger date;
@property (assign, nonatomic) NSInteger swing;
@property (assign, nonatomic) NSInteger duration;

@end

@implementation JCClubSetupVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCode];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if (self.dismissHandle) {
        self.dismissHandle();
    }
}

- (void)initCode{
    [_dateSlider setThumbImage:[UIImage imageNamed:@"club_setup_slider_thumb"] forState:UIControlStateNormal];
    [_swingSlider setThumbImage:[UIImage imageNamed:@"club_setup_slider_thumb"] forState:UIControlStateNormal];
    [_durationSlider setThumbImage:[UIImage imageNamed:@"club_setup_slider_thumb"] forState:UIControlStateNormal];
    [_dateSlider setThumbImage:[UIImage imageNamed:@"club_setup_slider_thumb"] forState:UIControlStateHighlighted];
    [_swingSlider setThumbImage:[UIImage imageNamed:@"club_setup_slider_thumb"] forState:UIControlStateHighlighted];
    [_durationSlider setThumbImage:[UIImage imageNamed:@"club_setup_slider_thumb"] forState:UIControlStateHighlighted];
    
    _dateSlider.minimumValue = kClubSetupPlainDateMin;
    _dateSlider.maximumValue = kClubSetupPlainDateMax;
    _swingSlider.minimumValue = kClubSetupPlainSwingMin;
    _swingSlider.maximumValue = kClubSetupPlainSwingMax;
    _durationSlider.minimumValue = kClubSetupPlainDurationMin;
    _durationSlider.maximumValue = kClubSetupPlainDurationMax;
    
    _submitBtn.layer.cornerRadius = 3;
    _submitBtn.layer.masksToBounds = YES;
    
    self.navigationItem.title = @"Movement Plan";
    
    if (_defaultValueItem) {
        _date = _defaultValueItem.activeDays.integerValue;
        _swing = _defaultValueItem.swings.integerValue;
        _duration = _defaultValueItem.duration.integerValue;
        [_dateSlider setValue:_date animated:YES];
        [_swingSlider setValue:_swing animated:YES];
        [_durationSlider setValue:_duration animated:YES];
        _dateValueLabel.text = @(_date).stringValue;
        _swingValueLabel.text = @(_swing).stringValue;
        if (_duration < 60) {
            _durationValueLabel.text = [NSString stringWithFormat:@"%ldmin",_duration%60];
        }else{
            _durationValueLabel.text = [NSString stringWithFormat:@"%ldh %ldmin",_duration/60,_duration%60];
        }
    }
}

- (void)setDefaultValue{
    [_dateSlider setValue:kClubSetupPlainDateDefault animated:YES];
    [_swingSlider setValue:kClubSetupPlainSwingDefault animated:YES];
    [_durationSlider setValue:kClubSetupPlainDurationDefault animated:YES];
    
    _date = kClubSetupPlainDateDefault;
    _swing = kClubSetupPlainSwingDefault;
    _duration = kClubSetupPlainDurationDefault;
    _dateValueLabel.text = @(_date).stringValue;
    _swingValueLabel.text = @(_swing).stringValue;
    if (_duration < 60) {
        _durationValueLabel.text = [NSString stringWithFormat:@"%ldmin",_duration%60];
    }else{
        _durationValueLabel.text = [NSString stringWithFormat:@"%ldh %ldmin",_duration/60,_duration%60];
    }
}

- (IBAction)dateSliderAction:(JCSlider *)sender {
    _date = (NSInteger)(sender.value / kClubSetupPlainDateScale) * kClubSetupPlainDateScale;
    _dateValueLabel.text = @(_date).stringValue;
}

- (IBAction)swingSliderAction:(JCSlider *)sender {
    _swing = (NSInteger)(sender.value / kClubSetupPlainSwingScale) * kClubSetupPlainSwingScale;
    _swingValueLabel.text = @(_swing).stringValue;
}

- (IBAction)durationSliderAction:(JCSlider *)sender {
    _duration = ((NSInteger)(sender.value / kClubSetupPlainDurationScale)) * kClubSetupPlainDurationScale;
    if (_duration < 60) {
        _durationValueLabel.text = [NSString stringWithFormat:@"%ldmin",_duration%60];
    }else{
        _durationValueLabel.text = [NSString stringWithFormat:@"%ldh %ldmin",_duration/60,_duration%60];
    }
}

- (IBAction)useDefaultAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    [self setDefaultValue];
    _dateSlider.enabled =
    _swingSlider.enabled =
    _durationSlider.enabled = !sender.selected;
}

- (IBAction)submitAction:(UIButton *)sender {
    __weak typeof(self)weakSelf = self;
    [self.hud showWithMaskType:WSProgressHUDMaskTypeClear];
    [JCClubApi setClubDate:_date swing:_swing duration:_duration clubID:_clubID complete:^(NSError *error) {
        if (error) {
            [weakSelf.hud showErrorWithString:error.domain dimissAfterSecond:2];
        }else{
            [weakSelf.hud dismiss];
            weakSelf.defaultValueItem.activeDays = @(weakSelf.date).stringValue;
            weakSelf.defaultValueItem.swings = @(weakSelf.swing).stringValue;
            weakSelf.defaultValueItem.duration = @(weakSelf.duration).stringValue;
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end






@implementation JCSlider

- (CGRect)trackRectForBounds:(CGRect)bounds{
    [super trackRectForBounds:bounds];
    bounds.size.height = 6;
    return bounds;
}

@end
